
applicants = read.csv("./scores_2018/data/analysis_df/applicants_merge_fixed.csv") %>% mutate(job_id = FORMASI)

##selecting variables
applicants_merge =
  applicants %>%
  mutate(skb_score = NILAI_SKB,
         p1_admin_selection =STATUS_VERIFICATION) %>%
  group_by(job_id) %>%
  mutate(num_jobs = sum(got_job, na.rm = T)) %>% 
  ungroup()


#making treatments and forcing vars

#T3, national threshold
applicants_merge %<>%
  mutate(t3 = case_when(skd_score >= 255 ~ 1,
                        skd_score < 255 ~ 0,
                        TRUE ~ NA_real_),
         forcing_t3 = skd_score - 255)

#T2 
applicants_merge %<>%
  group_by(job_id) %>%
  arrange(desc(skd_score)) %>%
  mutate(position_t2 = seq_along(skd_score)) %>%
  mutate(t2_tmp = (position_t2 <= TOTAL_FORMATION*3)*1,
         t2 = case_when(t2_tmp == 1 & t3 == 1 ~ 1,
                        t2_tmp == 0 & t3 == 1 ~ 0,
                        TRUE ~ NA_real_)) %>%
  select(-t2_tmp) %>%
  mutate(num_types = length(na.omit(unique(t2)))) %>%
  
  ungroup() %>%
  mutate(skd_t2_tmp = case_when(t2 %in% c(0,1) ~ as.numeric(skd_score),
                                TRUE ~ NA_real_)) %>%
  
  group_by(job_id, t2) %>%
  mutate(min_winner_t2 = case_when(num_types > 1 & !is.na(skd_t2_tmp) ~ min(skd_t2_tmp, na.rm = T),
                                   TRUE ~ NA_real_),
         max_loser_t2 = case_when(num_types > 1 & !is.na(skd_t2_tmp) ~ max(skd_t2_tmp, na.rm = T),
                                  TRUE ~ NA_real_)) %>%
  ungroup() %>%
  group_by(job_id) %>%
  mutate(min_winner_t2 = max(min_winner_t2, na.rm = T),
         max_loser_t2 = min(max_loser_t2, na.rm = T)) %>%
  ungroup() %>%
  
  mutate(forcing_t2 = case_when(t2 == 1 ~ skd_score - max_loser_t2,
                                t2 == 0 ~ skd_score - min_winner_t2,
                                TRUE ~ NA_real_)) %>%
  arrange(job_id)


#T1, get job
applicants_merge %<>%
  group_by(job_id) %>%
  arrange(desc(final_score)) %>%
  mutate(position_t1 = seq_along(final_score)) %>%
  ungroup() %>%
  mutate(t1 = (position_t1 <= TOTAL_FORMATION)*1) %>%
  # arrange(job_id)
  
  
  group_by(job_id, t1) %>%
  mutate(min_winner = min(final_score, na.rm = T),
         max_loser = max(final_score, na.rm = T)) %>%
  ungroup() %>%
  
  group_by(job_id) %>%
  mutate(min_winner = max(min_winner),
         max_loser = min(max_loser)) %>%
  ungroup() %>%
  mutate(forcing_t1 = case_when(t1 == 1 ~ final_score - max_loser,
                                t1 == 0 ~ final_score - min_winner,
                                TRUE ~ NA_real_)) %>%
  arrange(job_id)


applicants_merge %<>%
  select(-c(skd_t2_tmp, min_winner_t2, max_loser_t2, num_types, min_winner, max_loser))

applicants_full = applicants_merge

write.csv(applicants_merge, "./BKN survey/replication_file/data/applicants_full.csv")
